package vn.fpt.fsoft.dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import vn.fpt.fsoft.entity.*;
import vn.fpt.fsoft.util.HibernateUtil;

/*
 * Class TrustRegionDAO
 *
 * Version 1.0
 *
 * 22-August-2014
 *
 * Modification Logs:
 * DATE					AUTHOR				DESCRIPTION
 * --------------------------------------------------------
 * 22-August-2014		XuanTuan 			Create TrustRegionDAO
 */
public class TrustRegionDAO {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    /**
     * Constructor
     */
    public TrustRegionDAO() {
        this.sessionFactory = HibernateUtil.getSessionFactory();
        this.session = null;
        this.transaction = null;
    }

    /**
     * @return list all trust region
     */
    @SuppressWarnings("unchecked")
    public List<TrustRegion> getAllTrustRegion() {
        session = sessionFactory.openSession();
        List<TrustRegion> list = null;

        try {
            transaction = session.beginTransaction();
            list = session.createCriteria(TrustRegion.class).list();
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction == null)
                transaction.rollback();
            e.printStackTrace();
        } finally {
                session.close();
        }
        return list;
    }

    /**
     * @param trustRegionID
     * @return TrustRegion with trustRegionID
     */
    public TrustRegion getTrustRegionByKey(int trustRegionID) {
        session = sessionFactory.openSession();
        TrustRegion trustRegion = null;
    
        try {
            transaction = session.beginTransaction();
            trustRegion = (TrustRegion) session.get(TrustRegion.class,
                    trustRegionID);
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction == null)
                transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
        return trustRegion;
    }

    /**
     * @param trustRegion
     * @return true if addTrustRegion success else return false
     */
    public boolean addTrustRegion(TrustRegion trustRegion) {
        session = sessionFactory.openSession();

        try {
            transaction = session.beginTransaction();
            session.save(trustRegion);
            transaction.commit();
            return true;
        } catch (HibernateException e) {
            if (transaction == null)
                transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
        return false;
    }

    /**
     * @param trustRegion
     * @return true if amendTrustRegion success else return false
     */
    public boolean amendTrustRegion(TrustRegion trustRegion) {
        session = sessionFactory.openSession();

        try {
            transaction = session.beginTransaction();
            session.update(trustRegion);
            transaction.commit();
            return true;
        } catch (HibernateException e) {
            if (transaction == null) {
                transaction.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return false;
    }
}
